//
//  SerFirstViewController.m
//  A段项目
//
//  Created by dlios on 15/7/10.
//  Copyright (c) 2015年 dlios. All rights reserved.
//

#import "SerFirstViewController.h"
#import "SellRankViewController.h"
#import "BaseHeader.h"
#import "SANetWorkingTask.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "recModel.h"
#import "AllCarModel.h"
#import "SpecialCarModel.h"
#import "CarRecTableViewCell.h"
#import "AllCarTableViewCell.h"
#import "SellRankDetailViewController.h"
#import "smallTableModel.h"
#import "SmallTableViewCell.h"
#import "NewCarViewController.h"

@interface SerFirstViewController ()<UITableViewDataSource,UITableViewDelegate>

@property  (nonatomic, retain)UITableView *bigTable;
@property  (nonatomic, retain) NSMutableArray *recArr;
@property  (nonatomic, retain) NSMutableArray *carArr;
@property  (nonatomic, retain) NSMutableArray *recSarr;
@property  (nonatomic, retain) NSMutableArray *brandArr;
@property  (nonatomic, retain) NSMutableArray *section;
@property  (nonatomic, retain) UITableView *smallTable;
@property  (nonatomic, retain) UIView *listView;
@property  (nonatomic, retain) NSMutableArray *smallArr;
@property  (nonatomic, retain) UIImageView *intro;


@end

@implementation SerFirstViewController


- (void)dealloc
{
    [_carArr release];
    [_bigTable release];
    [_recArr release];
    [_recSarr release];
    [_listView release];
    [_smallTable release];
    [_intro release];
    [super dealloc];
}


-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        self.recArr = [NSMutableArray array];
        self.carArr = [NSMutableArray array];
        self.recSarr = [NSMutableArray array];
        self.brandArr = [NSMutableArray array];
        self.section = [NSMutableArray array];
        self.smallArr = [NSMutableArray array];
        
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"找车";
    // Do any additional setup after loading the view.
    
    
    [SANetWorkingTask request:@"http://api.ycapp.yiche.com/car/getmasterbrandlist?allmasterbrand=false " parmater:nil block:^(NSDictionary *result) {
        NSMutableArray *arr = [result objectForKey:@"data"];
        for (NSDictionary *dic in arr)
        {
            AllCarModel *carmodel = [[AllCarModel alloc] init];
            [carmodel setValuesForKeysWithDictionary:dic];
            [self.carArr addObject:carmodel];
            
            
        }
        [self makesection];
        
        [self.bigTable reloadData];
    }];
    
    [SANetWorkingTask request:@"http://api.ycapp.yiche.com/yicheapp/GetSelectCarPageAd?platform=2" parmater:nil block:^(NSDictionary *result) {
        
        NSDictionary *bigdic = [result objectForKey:@"data"];
        NSMutableArray *arr = [bigdic objectForKey:@"hotMaster"];
        NSMutableArray *arr1 = [bigdic objectForKey:@"hotSerial"];
        
        
        for (NSDictionary *dic in arr) {
            recModel *recmodel = [[recModel alloc] init];
            [recmodel setValuesForKeysWithDictionary:dic];
            [self.recArr addObject:recmodel];
        }
        for (NSDictionary *dic1  in arr1) {
            SpecialCarModel *carsmodel = [[SpecialCarModel alloc] init];
            [carsmodel setValuesForKeysWithDictionary:dic1];
            [self.recSarr addObject:carsmodel];
            
            
            
        }
        
        
        
        [self.bigTable reloadData];
        
    }];
    [self creatView];
    [self creatTable];
    
    
    self.listView = [[UIView alloc] initWithFrame:CGRectMake(ScreenWidth, 0, ScreenWidth, ScreenHeidth)];
    _listView.layer.borderWidth = 1;
    [self.view addSubview:self.listView];
    self.listView.layer.borderColor = [[UIColor grayColor] CGColor];
    
    self.smallTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeidth- ScreenHeidth/667*106) style:UITableViewStylePlain];
        self.smallTable.dataSource = self;
        self.smallTable.delegate = self;
    [self.smallTable registerClass:[SmallTableViewCell class] forCellReuseIdentifier:@"cc"];
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeidth/667*80)];
    self.intro = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenWidth/375*10, ScreenHeidth/667*10, ScreenWidth/375*70, ScreenHeidth/667*60)];
    [self.intro setBackgroundColor:[UIColor whiteColor]];
    [view addSubview:self.intro];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth/375*120, ScreenHeidth/667*30, ScreenWidth/375*100,ScreenHeidth/667* 20)];
   label.text = @"品牌介绍";
    label.font = [UIFont systemFontOfSize:18];
    [view addSubview:label];
    self.smallTable.tableHeaderView = view;

    [self.listView addSubview:self.smallTable];
    
    
}

- (void)creatView
{
    // 销量排行
    UIImageView *rank = [[UIImageView alloc] initWithFrame:CGRectMake(70, 40, 60, 60)];
    [rank setImage:[UIImage imageNamed:@"xiaoliang.png"]];
    rank.userInteractionEnabled = YES;
    [self.view addSubview:rank];
    // 给图片添加手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [rank addGestureRecognizer:tap];
    UILabel *rankl = [[UILabel alloc] initWithFrame:CGRectMake(70, 105, 60, 20)];
    rankl.text = @"销量排行";
    rankl.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:rankl];
    
    //新车
    UIImageView *newcar = [[UIImageView alloc] initWithFrame:CGRectMake(210, 40, 60, 60)];
    newcar.userInteractionEnabled = YES;
    [newcar setImage:[UIImage imageNamed:@"shangshi.png"]];
    UITapGestureRecognizer *tap1= [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap1Action:)];
    [newcar addGestureRecognizer:tap1];
    [self.view addSubview:newcar];
    [newcar release];
    UILabel *newl = [[UILabel alloc] initWithFrame:CGRectMake(210, 105, 60, 20)];
    newl.text = @"新车上市";
    newl.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:newl];
    
    
    
    
}
- (void)tap1Action:(UITapGestureRecognizer *)tap
{
    NewCarViewController  *new = [[NewCarViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:new];
    [self presentViewController:nav animated:YES completion:nil];
    
    [new release];
    
   
}
/**
 进入销售排行界面
 */
- (void)tapAction:(UITapGestureRecognizer *)tap
{
    SellRankViewController *sell = [[SellRankViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:sell];
    [self presentViewController:nav animated:YES completion:nil];
    [nav release];
    [sell release];
    
}
/**
 *   创建整个界面的tableview
 */
- (void)creatTable
{
    self.bigTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 140, ScreenWidth, ScreenHeidth-250) style:UITableViewStylePlain];
    self.bigTable.delegate = self;
    self.bigTable.dataSource = self;
    
    [_bigTable registerClass:[CarRecTableViewCell class] forCellReuseIdentifier:@"cc"];
    [_bigTable registerClass:[AllCarTableViewCell class] forCellReuseIdentifier:@"bb"];
    [self.view addSubview:self.bigTable];
    
}

#pragma mark - 确定大taobleview的setion个数
- (void)makesection
{
    
    for (AllCarModel *model in self.carArr) {
        
        if (self.section.count == 0) {
            NSMutableDictionary *dic = [NSMutableDictionary dictionary];
            NSMutableArray *array = [NSMutableArray array];
            [array addObject:model];
            [dic setValue:model.initial forKey:@"title"];
            [dic setValue:array forKey:@"array"];
            
            [self.section addObject:dic];
        }else{
            for (int i = 0; i<self.section.count; i++) {
                NSString *str = model.initial;
                if ([str isEqualToString:[[self.section lastObject]  objectForKey:@"title"]]) {
                    
                    
                    NSMutableArray *array = [[self.section lastObject] objectForKey:@"array"];
                    [array addObject:model];
                }else{
                    
                    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
                    NSMutableArray *array=[NSMutableArray array];
                    [array addObject:model];
                    [dic setValue:model.initial forKey:@"title"];
                    [dic setValue:array forKey:@"array"];
                    [self.section addObject:dic];
                }
                break;
            }
        }
    }
}


#pragma mark - tableview两个协议
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.bigTable) {
        
        if (section == 0) {
            return 1;
        }else{
            NSDictionary *dic = [self.section objectAtIndex:section-1] ;
            NSArray *arr = [dic objectForKey:@"array"];
            
            return arr.count;
        }
    }else
    {
        
        smallTableModel *model = [self.smallArr objectAtIndex:section];
        NSArray *arr = model.serialList;
        
        return arr.count;
    }
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    
    
    if (tableView == self.bigTable) {
        
        return self.section.count+1;
    } else {
        return self.smallArr.count;
    }
}



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (tableView == self.bigTable) {
        
    
        if (indexPath.section == 0) {
            
            CarRecTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cc"];
            if (self.recArr.count != 0) {
                
                
                recModel *model1 = [self.recArr objectAtIndex:0];
                [cell.carimage1 sd_setImageWithURL:[NSURL URLWithString:model1.pic_url] placeholderImage:[UIImage imageNamed:@"1.jpg"]];
                UITapGestureRecognizer *touch1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(smallAction:)];
                [cell.carimage1 addGestureRecognizer:touch1];
                [touch1 release];
                cell.label1.text = model1.masterName;
                
                
                recModel *model2 = [self.recArr objectAtIndex:1];
                [cell.carimage2 sd_setImageWithURL:[NSURL URLWithString:model2.pic_url] placeholderImage:[UIImage imageNamed:@"1.jpg"]];
                UITapGestureRecognizer *touch2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(smallAction:)];
                [cell.carimage2 addGestureRecognizer:touch2];
                [touch2 release];
                cell.label2.text = model2.masterName;
                
                
                recModel *model3 = [self.recArr objectAtIndex:2];
                cell.label3.text = model3.masterName;
                [cell.carimage3 sd_setImageWithURL:[NSURL URLWithString:model3.pic_url] placeholderImage:[UIImage imageNamed:@"1.jpg"]];
                UITapGestureRecognizer *touch3 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(smallAction:)];
                [cell.carimage3 addGestureRecognizer:touch3];
                [touch3 release];

                recModel *model4 = [self.recArr objectAtIndex:3];
                cell.label4.text = model4.masterName;
                [cell.carimage4 sd_setImageWithURL:[NSURL URLWithString:model4.pic_url] placeholderImage:[UIImage imageNamed:@"1.jpg"]];
                UITapGestureRecognizer *touch4 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(smallAction:)];
                [cell.carimage4 addGestureRecognizer:touch4];
                [touch4 release];

                recModel *model5 = [self.recArr objectAtIndex:4];
                cell.label5.text = model5.masterName;
                [cell.carimage5 sd_setImageWithURL:[NSURL URLWithString:model5.pic_url] placeholderImage:[UIImage imageNamed:@"1.jpg"]];
                UITapGestureRecognizer *touch5 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(smallAction:)];
                [cell.carimage5 addGestureRecognizer:touch5];
                [touch5 release];

                /**
                 *  推荐大图
                 */
                SpecialCarModel *specialModel = [self.recSarr objectAtIndex:0];
                [cell.bigCarimage1 sd_setImageWithURL:[NSURL URLWithString:specialModel.pic_url] placeholderImage:[UIImage imageNamed:@"1,jpg"]];
                UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(detailAction:)];
                cell.bigCarimage1.tag = 1;
                [cell.bigCarimage1 addGestureRecognizer:tap];
                
                
                cell.carlabel1.text = specialModel.serialName;
                SpecialCarModel *specialModel1 = [self.recSarr objectAtIndex:1];
                [cell.bigCarimage2 sd_setImageWithURL:[NSURL URLWithString:specialModel1.pic_url] placeholderImage:[UIImage imageNamed:@"1,jpg"]];
                
                UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(detailAction:)];
                cell.bigCarimage2.tag = 2;
                [cell.bigCarimage2 addGestureRecognizer:tap1];
                cell.carlabel2.text = specialModel1.serialName;
                
                
                SpecialCarModel *specialModel2 = [self.recSarr objectAtIndex:2];
                
                [cell.bigCarimage3 sd_setImageWithURL:[NSURL URLWithString:specialModel2.pic_url] placeholderImage:[UIImage imageNamed:@"1,jpg"]];
                UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(detailAction:)];
                cell.bigCarimage3.tag = 3;
                [cell.bigCarimage3 addGestureRecognizer:tap2];
                cell.carlabel3.text = specialModel2.serialName;
                
                
                
            }
            
            
            return cell;
        }else{
            AllCarTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"bb"];
            NSDictionary *dic = [self.section objectAtIndex:indexPath.section -1];
            NSArray *arr = [dic objectForKey:@"array"];
            AllCarModel *model = [arr objectAtIndex:indexPath.row];
            [cell.image sd_setImageWithURL:[NSURL URLWithString:model.logoUrl] placeholderImage:[UIImage imageNamed:@"1.jpg"]];
            cell.nameLabel.text = model.name;
           
            
            
            return cell;
            
        }
    }
    else {
        SmallTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cc"];
        smallTableModel *model = [self.smallArr objectAtIndex:indexPath.section];
        NSArray *arr = model.serialList;
        NSDictionary *dic = [arr objectAtIndex:indexPath.row];
        NSString *str = [dic objectForKey:@"Picture"];
        if ([str containsString:@"{0}"]) {
            NSRange range = [str rangeOfString:@"{0}"];
            NSMutableString *str1 = [str mutableCopy];
            [str1 replaceCharactersInRange:range withString:@"3"];
            str = str1;
        }

        [cell.image sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"1.jpg"]];
        cell.nameLabel.text = [dic objectForKey:@"serialName"];
        
        if ([[dic objectForKey:@"dealerPrice"] isEqualToString:@"未上市"]) {
            cell.stateLabel.text = [dic objectForKey:@"dealerPrice"];
            cell.stateLabel.textColor = [UIColor grayColor];
            
        }else{
            cell.stateLabel.text = [dic objectForKey:@"dealerPrice"];
            cell.stateLabel.textColor = [UIColor redColor];
        }
        
        
        return cell;
    }
}







- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.bigTable) {
        
        if (indexPath.section == 0) {
            return 220;
        }else
        {
            return 70;
        }
    }else{
        return 90;
    }
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (tableView == self.bigTable) {
        
        if (section == 0) {
            return nil;
        }else
        {
            NSDictionary *dic = [self.section objectAtIndex:section -1];
            return [dic objectForKey:@"title"];
        }
    }else{
        smallTableModel *model = [self.smallArr objectAtIndex:section];
        return model.brandName;
        
      
        
    }
    
    
}
#pragma mark - bigtableview的点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (tableView == self.bigTable) {
        if (indexPath.section == 0) {
            
        }else{
        
        
        NSDictionary *dic1 = [self.section objectAtIndex:indexPath.section -1];
        NSArray *arr1 = [dic1 objectForKey:@"array"];
        AllCarModel *model1 = [arr1 objectAtIndex:indexPath.row];
        
         [self.intro sd_setImageWithURL:[NSURL URLWithString:model1.logoUrl] placeholderImage:[UIImage imageNamed:@"1.jpg"]];
        
        __block SerFirstViewController *first = self;
            [UIView animateWithDuration:0.5 animations:^{
                first.listView.frame = CGRectMake(90, 0, ScreenWidth, ScreenHeidth);
                
            }];
        NSDictionary *dic = [self.section objectAtIndex:indexPath.section-1];
        NSArray *arr = [dic objectForKey:@"array"];
        AllCarModel *model = [arr objectAtIndex:indexPath.row];
        
        
        
        NSString *web = [NSString stringWithFormat:@"http://api.ycapp.yiche.com/car/getseriallist?masterid=%ld&allserial=false",model.masterId];
       
        [SANetWorkingTask request:web parmater:nil block:^(NSDictionary *result) {
            NSArray *arr = [result objectForKey:@"data"];
            self.smallArr = [NSMutableArray array];
            for (NSDictionary *dic in arr) {
                smallTableModel *model = [[smallTableModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [self.smallArr addObject:model];
            }
            [self.smallTable reloadData];
        }];
        }
    }else
    {
        smallTableModel *model = [self.smallArr objectAtIndex:indexPath.section];
        NSArray *arr = model.serialList;
        NSDictionary *dic = [arr objectAtIndex:indexPath.row];

        SellRankDetailViewController *rank = [[SellRankDetailViewController alloc] init];
        rank.website = [dic objectForKey:@"serialId"];
        rank.name = [dic objectForKey:@"serialName"];
        rank.pic = [dic objectForKey:@"Picture"];
        rank.dealerPrice = [dic objectForKey:@"dealerPrice"];
        
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:rank];
        
        
        [self presentViewController:nav animated:YES completion:nil];
        
    }
    
}

#pragma mark - 添加索引
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    if (tableView == self.bigTable) {
        
            
            NSMutableArray *arr = [NSMutableArray array];
        [arr addObject:@" "];
            for (NSDictionary *dic in self.section) {
                [arr addObject:[dic objectForKey:@"title"]];
            
        }
        return arr;
    }
    return nil;
    
    
}



#pragma mark - mark setion＝0里图片的点击事件
- (void)detailAction:(UITapGestureRecognizer *)tap
{
    if (tap.view.tag == 1) {
        SpecialCarModel *specialModel = [self.recSarr objectAtIndex:0];
        
        SellRankDetailViewController *rank = [[SellRankDetailViewController alloc] init];
        rank.website = specialModel.serialId;
        rank.name = specialModel.serialName;
        rank.pic = specialModel.pic_url;
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:rank];
        
        
        [self presentViewController:nav animated:YES completion:nil];
    }if (tap.view.tag == 2) {
        SpecialCarModel *specialModel = [self.recSarr objectAtIndex:1];
        
        SellRankDetailViewController *rank = [[SellRankDetailViewController alloc] init];
        rank.website = specialModel.serialId;
        rank.name = specialModel.serialName;
        rank.pic = specialModel.pic_url;

        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:rank];
        
        
        [self presentViewController:nav animated:YES completion:nil];
        
    }if (tap.view.tag ==3) {
        SpecialCarModel *specialModel = [self.recSarr objectAtIndex:2];
        
        SellRankDetailViewController *rank = [[SellRankDetailViewController alloc] init];
        rank.website = specialModel.serialId;
        rank.name = specialModel.serialName;
        rank.pic = specialModel.pic_url;

        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:rank];
        
        
        [self presentViewController:nav animated:YES completion:nil];
        
    }
    
}

#pragma mark - tableview滑动时收起边栏
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    __block SerFirstViewController *first = self;
    if (scrollView == self.bigTable) {
        [UIView animateWithDuration:0.5 animations:^{
            first.listView.frame = CGRectMake(ScreenWidth, 0, ScreenWidth, ScreenHeidth);
            
        }];
    }
    
    
}

- (void)smallAction:(UITapGestureRecognizer *)tap
{
    
    recModel *model = [self.recArr objectAtIndex:tap.view.tag-1];
    [self.intro sd_setImageWithURL:[NSURL URLWithString:model.pic_url] placeholderImage:[UIImage imageNamed:@"1.jpg"]];

    [SANetWorkingTask request:[NSString stringWithFormat:@"http://api.ycapp.yiche.com/car/getseriallist?masterid=%ld&allserial=false",model.masterId] parmater:nil block:^(NSDictionary *result) {
        NSArray *arr = [result objectForKey:@"data"];
        self.smallArr = [NSMutableArray array];
        for (NSDictionary *dic in arr) {
            smallTableModel *model = [[smallTableModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.smallArr addObject:model];
    }
        [self.smallTable reloadData];
    }];
    
    
    __block SerFirstViewController *first = self;
    [UIView animateWithDuration:0.5 animations:^{
        first.listView.frame = CGRectMake(90, 0, ScreenWidth, ScreenHeidth);
        
    }];

    
}






@end
